﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 自制数学工具;
using Point = 自制数学工具.Point;

namespace 求平行坐标点
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Point A = new Point();
            Point B = new Point();
            Point O = new Point();
            try
            {
                char[] seperator = new char[] { ',','，','(',')','（','）' };
                string[] a = txtPointA.Text.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
                A.X = double.Parse(a[0]); A.Y = double.Parse(a[1]);
                string[] b = txtPointB.Text.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
                B.X = double.Parse(b[0]); B.Y = double.Parse(b[1]);
                string[] o = txtPointO.Text.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
                O.X = double.Parse(o[0]); O.Y = double.Parse(o[1]);
                double Loy = double.Parse(txtOY.Text);
                double Lgy = double.Parse(txtGY.Text);
                double Lfy = double.Parse(txtFY.Text);
                double Lcd = double.Parse(txtCD.Text);
                Vector OA = new Vector(O, A);
                Vector OB = new Vector(O, B);
                if (OA.X * OB.Y - OA.Y * OB.X == 0)
                {
                    throw new Exception("参数异常，无法构成三角形！");
                }
                if (Lfy > Lcd/2 || Lgy > Lcd/2)
                {
                    throw new Exception("参数异常，G/F位置超长");
                }
                double Loa = OA.Length();double Lob = OB.Length();
                if(Loa*Loa< Loy * Loy + Lfy * Lfy || Lob * Lob < Loy * Loy + Lgy * Lgy)
                {
                    throw new Exception("参数异常，G/F位置超长");
                }
                double oa = Math.Atan2(OA.Y, OA.X);
                double ob = Math.Atan2(OB.Y, OB.X);
                double oy = (oa + ob) / 2;double rotation = 0;
                Point Y = new Point();
                if (oa > ob)
                {
                    rotation = (oy - ob) * 180 / Math.PI;
                    Y = Maths.GetRotationPoint(O, B, rotation, Loy);
                }
                else
                {
                    rotation = (oy - oa) * 180 / Math.PI;
                    Y = Maths.GetRotationPoint(O, A, rotation, Loy);
                }
                Point F = Maths.GetRotationPoint(Y, O, -90, Lfy);
                Point G = Maths.GetRotationPoint(Y, O, 90, Lgy);
                Point E = Maths.GetExtendPoint(A, O, -Lfy / Math.Sin(rotation / 180 * Math.PI));
                Point H = Maths.GetExtendPoint(B, O, -Lgy / Math.Sin(rotation / 180 * Math.PI));

                F.X = Math.Round(F.X, 2);F.Y = Math.Round(F.Y, 2);
                G.X = Math.Round(G.X, 2);G.Y = Math.Round(G.Y, 2);
                E.X = Math.Round(E.X, 2);E.Y = Math.Round(E.Y, 2);
                H.X = Math.Round(H.X, 2);H.Y = Math.Round(H.Y, 2);

                listBox1.Items.Clear();
                listBox1.Items.Add($"F ({F.X}, {F.Y})");
                listBox1.Items.Add($"G ({G.X}, {G.Y})");
                listBox1.Items.Add($"E ({E.X}, {E.Y})");
                listBox1.Items.Add($"H ({H.X}, {H.Y})");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

    }
}
